package com.edu.dao.impl;

import com.edu.dao.BaseDao;
import com.edu.dao.UserDao;
import com.edu.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author:czc
 * @Date: 2024/9/13 19:22
 * @Description:
 */
public class UserDaoImpl extends BaseDao implements UserDao {

    @Override
    public User selectUserByPass(String username, String userpass) {
        Connection conn= BaseDao.getConn();
		PreparedStatement s = null;
		ResultSet rs = null;
		try {
			if(conn!=null) {
				System.out.println("连接成功");
			}
			String sql = "select * from user where username='"+username+"' and userpass='"+userpass+"'";
			System.out.println(sql);
			s = conn.prepareStatement(sql);
			rs =  s.executeQuery();
			while(rs.next()) {
				User user = new User();
				user.setUserId(rs.getInt("userId"));
				user.setUsername(rs.getString("username"));
				user.setUserpass(rs.getString("userpass"));
				user.setName(rs.getString("name"));
				return user;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(conn, s,rs);
		}
		return null;
    }

    @Override
    public boolean selectUser(String username) {
        Connection conn= BaseDao.getConn();
        PreparedStatement s = null;
        ResultSet rs = null;
        try {
            if(conn!=null) {
                System.out.println("连接成功");
            }
            String sql = "select * from user where username='"+username+"'";
            System.out.println(sql);
            s = conn.prepareStatement(sql);
            rs =  s.executeQuery();
            while(rs.next()) {
                return true;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(conn, s,rs);
        }
        return false;
    }

    @Override
    public int addUser(String username, String userpass, String name) {
        Object[] parm= {username,userpass,name};
        String sql="INSERT INTO user(username,userpass,name) VALUES(?,?,?)";
        return super.executeUpdate(sql,parm);
    }
}
